package com.fjwt.gz.service.activity.game.acte001;

import com.fjwt.gz.core.constants.Constants;
import com.fjwt.gz.core.exception.BizException;
import com.fjwt.gz.db.entity.ActivityAwardEntity;
import com.fjwt.gz.db.entity.ActivityEntity;
import com.fjwt.gz.db.entity.SysExportTaskEntity;
import com.fjwt.gz.db.vo.ExportAwardVO;
import com.fjwt.gz.service.activity.AbstractExportReport;
import com.fjwt.gz.service.impl.ActivityAwardService;
import com.fjwt.gz.service.impl.FansAwardService;
import com.fjwt.gz.service.mapper.ScoreRecordTotalMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ------ 抽奖活动--商家版  导出报表
 */
@Slf4j
@Service
public class ACTE001ExportReport extends AbstractExportReport<ExportAwardVO> {

    @Autowired
    private ActivityAwardService activityAwardService;

    @Autowired
    private FansAwardService fansAwardService;

    @Autowired
    private ScoreRecordTotalMapper recordTotalMapper;


    private static final Map<String, String> HEADER_ALIAS_MAP = new HashMap<>();

    static {
        HEADER_ALIAS_MAP.put("fansId", "用户唯一ID");
        HEADER_ALIAS_MAP.put("realName", "参与客户姓名");
        HEADER_ALIAS_MAP.put("phone", "参与客户手机号");
        HEADER_ALIAS_MAP.put("isAuth", "参与客户是否实名");
        HEADER_ALIAS_MAP.put("couponName", "领用卡券名称");
        HEADER_ALIAS_MAP.put("couponPrice", "卡券金额");
        HEADER_ALIAS_MAP.put("couponThirdId", "微信唯一卡券编号");
        HEADER_ALIAS_MAP.put("createdAt", "参与时间");
        HEADER_ALIAS_MAP.put("writeTime", "核销时间");
        HEADER_ALIAS_MAP.put("status", "卡券状态");
        HEADER_ALIAS_MAP.put("mchNo", "发放商户号");
        HEADER_ALIAS_MAP.put("mchName", "发放商户名称");

        HEADER_ALIAS_MAP.put("name", "福分名称");
        HEADER_ALIAS_MAP.put("score", "福分值");
        HEADER_ALIAS_MAP.put("mchChannel", "入链渠道");
        HEADER_ALIAS_MAP.put("channelCode", "入链账号");
        HEADER_ALIAS_MAP.put("channelName", "入链账号名称");
        HEADER_ALIAS_MAP.put("agentNo", "入链账号所属网点机构号");
    }


    @Override
    public String exportActivityExcel(SysExportTaskEntity sysExportTaskEntity) {
        ActivityEntity activityEntity = getRequestParam(sysExportTaskEntity.getRequestParam(), ActivityEntity.class);
        // 查询活动卡券
        List<ActivityAwardEntity> activityAwardList = activityAwardService.list(ActivityAwardEntity.gw()
                .eq(ActivityAwardEntity::getActivityId, activityEntity.getActivityId())
                .eq(ActivityAwardEntity::getAwardType, Constants.AWARD.COUPON));
        if (activityAwardList.isEmpty()) {
            throw new BizException("该活动未配置奖品！");
        }
        List<Long> couponIdList = new ArrayList<>();
        List<Long> goodsIdList = new ArrayList<>();
        List<Long> scoreIdList = new ArrayList<>();

        for (ActivityAwardEntity activityAward : activityAwardList) {
            if (activityAward.getAwardType() != null) {
                switch (activityAward.getAwardType()) {
                    case Constants.AWARD.COUPON:
                        couponIdList.add(activityAward.getAwardId());
                        break;
                    case Constants.AWARD.GOODS:
                        goodsIdList.add(activityAward.getAwardId());
                        break;
                    case Constants.AWARD.SCORE:
                        scoreIdList.add(activityAward.getAwardId());
                        break;
                    default:
                        break;
                }
            }
        }

        List<ExportAwardVO> resultList = new ArrayList<>();
        if (!couponIdList.isEmpty()) {
            List<ExportAwardVO> awardVOList = fansAwardService.exportCouponAward(couponIdList, activityEntity.getAgentNo());
            resultList.addAll(awardVOList);
        }
        if (!goodsIdList.isEmpty()) {
            List<ExportAwardVO> awardVOList = fansAwardService.exportGoodsAward(goodsIdList);
            resultList.addAll(awardVOList);
        }
        if (!scoreIdList.isEmpty()) {
            // 福分奖品
            List<ExportAwardVO> scoreAwardList = recordTotalMapper.exportScoreAward(activityEntity.getActivityId());
            resultList.addAll(scoreAwardList);
        }

        return createExcelAndUploadOssUrl(resultList, HEADER_ALIAS_MAP, sysExportTaskEntity);
    }
}
